package com.mawujun.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.Ordered;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
/**
 * 用于处理系统异常，业务异常最好自己包装然后抛出
 * @author mawujun
 *
 */
public class JsonSimpleMappingExceptionResolver implements
		HandlerExceptionResolver , Ordered{
	private int order = Integer.MAX_VALUE;  // default: same as non-Ordered

	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		response.setContentType("text/html");
		// TODO Auto-generated method stub
		StringBuilder result=new StringBuilder("");
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.append("{success:false,errors:{msg:'服务器端出现异常，请重新尝试或联系管理员'}}");
			writer.write(result.toString());
			writer.close();
			return null;
		} 
		result.append("{success:false,errors:{msg:'服务器端出现异常，请重新尝试或联系管理员'}}");
		writer.write(result.toString());
		writer.close();
		return null;
	}

	@Override
	public int getOrder() {
		// TODO Auto-generated method stub
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

}
